package com.kun.companion.core.http;

import cn.hutool.core.util.StrUtil;
import com.kun.companion.core.context.annotation.Component;
import com.kun.companion.core.context.annotation.PostConstruct;
import com.kun.companion.core.http.request.DeepSeekRequest;
import com.kun.companion.core.http.response.DeepSeekResponse;
import com.kun.companion.core.util.JacksonUtil;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author gzc
 * @since 2025/3/1
 **/
@Component
public class DeepSeekService {
    private static final String apiKey = "sk-2039b67059fd4f32a27b82539d0c6a92";
    private static final String url = "https://api.deepseek.com/beta/completions";
    private static final String chat_url = "https://api.deepseek.com/chat/completions";
    private static final String model = "deepseek-chat";

    private static OkHttpService HTTP_SERVICE;

    @PostConstruct
    public void init(OkHttpService okHttpService) {
        HTTP_SERVICE = okHttpService;
    }

    public static String sendAndResult(String userMessage) {
        DeepSeekResponse response = sendAndResponse(userMessage);
        if (response != null) {
            return response.getChoices().get(0).getMessage().getContent();
        }
        return "";
    }


    public static DeepSeekResponse sendAndResponse(String userMessage) {
        DeepSeekRequest deepSeekRequest = DeepSeekRequest.defaultRequest(userMessage);
        deepSeekRequest.setModel(model);
        Map<String, String> headerMap = new HashMap<>(3);
        headerMap.put("Content-Type", "application/json");
        headerMap.put("Accept", "application/json");
        headerMap.put("Authorization", "Bearer " + apiKey);
        DeepSeekResponse response = null;
        try {
//            String responseText = HTTP_SERVICE.post(chat_url, JacksonUtil.toJsonStr(deepSeekRequest), MediaType.parse("application/json"), headerMap);
            // 创建请求
            String requestBody = JacksonUtil.toJsonStr(deepSeekRequest);
            System.out.println(JacksonUtil.toJsonPrettyStr(deepSeekRequest));
            Request request = new Request.Builder()
                    .url(chat_url)
                    .post(RequestBody.create(requestBody, MediaType.parse("application/json")))
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json")
                    .addHeader("Authorization", "Bearer " + apiKey)
                    .build();
            String responseText = HTTP_SERVICE.execute(request);
            System.out.println(responseText);
            response = buildResponse(responseText);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    private static DeepSeekResponse buildResponse(String responseText) {
        if (StrUtil.isNotBlank(responseText)) {
           return JacksonUtil.parseObject(responseText, DeepSeekResponse.class);
        }
        return null;
    }
}
